package com.hxxy.wyp.petsLover.common.rest.base.fronted;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.News;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.HomeInfoVo;
import com.hxxy.wyp.petsLover.common.services.base.backend.NewsService;
import com.hxxy.wyp.petsLover.config.RedisConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName HomeInfoFrontRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/5 13:12
 * @Version 1.0
 */
@RestController
@RequestMapping("homeInfo")
public class HomeInfoFrontRest extends RestBase {

    @Autowired
    private NewsService newsService;
    @Autowired
    private RedisConfig redisConfig;

    @RequestMapping("selectNewsFront")
    public Object selectNewsFrontId() {
        Gson gson = new Gson();
        String info = (String) redisConfig.getRedisTemplateByDb(0).opsForValue().get("list");
        if (StringUtils.isNoneBlank(info)) {
            List<News> news = gson.fromJson(info, new TypeToken<List<News>>() {
            }.getType());
            return ajaxSuccess(news, "200");
        }

        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(News::getNewsIsSelected, "1");
        List<News> list = newsService.list(lambdaQueryWrapper);
        String s = gson.toJson(list);
        redisConfig.getRedisTemplateByDb(0).opsForValue().set("list", s);
        redisConfig.getRedisTemplateByDb(0).expire("list", Duration.ofMinutes(5L));
        return ajaxSuccess(list, "200");
    }


    @RequestMapping("selectNewsById")
    public Object selectNewsById() {
        News newsServiceById = newsService.getById(7);
        return ajaxSuccess(newsServiceById, "200", "查询成功!");
    }
}
